package com.ssyc.gsk_teacher_appraisal.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.IndicatorManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.vp.SuperViewPager;
import com.ssyc.gsk_teacher_appraisal.R;
import com.ssyc.gsk_teacher_appraisal.bean.ChooseTestInfo;
import com.ssyc.gsk_teacher_appraisal.fragment.TkTeacherChooseTestFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 教师端题库之选择套题
 * Created by Kevin on 2018/10/23
 */
public class TkTeacherAppraisalChooseTestActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack;

    private MagicIndicator mMag;
    private SuperViewPager mVp;
    private LinearLayout llContent;

    private RelativeLayout rlLoadingView;

    //数据集合
    private List<List<ChooseTestInfo.ListBean>> oldDatas;

    private String[] mTitles = {"2 - 1", "2 - 2", "2 - 3", "2 - 4", "2 - 5", "2 - 6"};
    //适配器对象
    private VpAdapter adapter;



    //用于填充ViewPager的Fragment集合
    private List<Fragment> mFragments;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_tk_teacher_appraisal_choose;
    }

    @Override
    protected void init() {
        showContent();

        //初始化视图
        initView();

        //初始化导航栏
        initMag();

    }

    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        mVp = findViewById(R.id.vp);
        mMag = findViewById(R.id.mag);
        llContent = findViewById(R.id.ll_content);
        rlLoadingView = findViewById(R.id.rl_loading);
        rlLoadingView.setVisibility(View.GONE);


    }


    /**
     * 初始化顶部的导航栏
     */
    private void initMag() {

//      IndicatorManager.setDynamicIndicator(this, mTitles, mVp, mMag, "#999999", "#F5AD3E");
        IndicatorManager.setIndicator(this, 18, mMag, mVp, mTitles, getResources().getColor(R.color.gary_text), getResources().getColor(R.color.tv_app_choose_item_bg) , null);

        //请求页面列表数据
        httpPageData();

    }


    /**
     * 请求页面列表数据
     */
    private void httpPageData() {

        Map<String, String> map = new HashMap<>();
        map.put("type", "1");
        map.put("ttel", AccountUtils.getAccount(BaseApplication.context));


        HttpUtils.post(HttpAdress.CPMain, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                ChooseTestInfo info = null;
                try {
                    info = GsonUtil.jsonToBean(response, ChooseTestInfo.class);
                } catch (Exception e) {
                    Log.i("test", "数据解析异常");
                }


                Log.i("test", "选择套题json：" + response);
                if (null != info) {
                    if ("200".equals(info.getState())) {
                        //返回成功
                        //初始化Vp
                        initVp(info);

                    } else {
                        //返回错误
                        UiUtils.Toast("服务器返回异常,状态码是:" + info.getState(), false);
                        Log.i("test", "服务器返回异常,状态码是:" + info.getState());
                    }
                } else {
                    Log.i("test", "服务器返回数据为空");
                }
            }
        });
    }

    /**
     * 初始化ViewPager的方法
     */
    private void initVp(ChooseTestInfo info) {
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {

            if (i < info.getList().size()) {
                mFragments.add(TkTeacherChooseTestFragment.newInstance(info, i));
            } else {
                mFragments.add(TkTeacherChooseTestFragment.newInstance(info, i));
            }
        }

        adapter = new VpAdapter(getSupportFragmentManager());
        mVp.setAdapter(adapter);
        mVp.setOffscreenPageLimit(mFragments.size());
    }


    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
//        else if (id == R.id.tv_submit) {
//            UiUtils.startActivity(this, TkTeacherAppraisalTestReleaseActivity.class);
//        }
    }


    /**
     * ViewPager的适配器
     */
    class VpAdapter extends FragmentPagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }


    /**
     * EventBus的处理事件的方法
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
         if(busInfo != null && TkTeacherAppraisalMainActivity.CPUPDATEPAGEDATA.equals(busInfo.msg)){
            //提交成功关闭
            finish();
        }
    }
}
